const router = require('koa-router')()

const { SuccessModel, ErrorModel } = require('../model/resModel')
const { login } = require('../controller/user')

router.prefix('/api/user')

/* GET users listing. */
router.post('/login', async (ctx, next) => {
  const { username, password } = ctx.request.body
  const data = await login(username, password)
  
  if (data.username) {
    // 设置session
    ctx.session.username = data.username
    ctx.session.realname = data.realname

    ctx.body = new SuccessModel()
    return
  }
  ctx.body = new ErrorModel('login failed')
});

module.exports = router
